package com.woniuxy.cloud.starter.context;



import com.ostrich.cloud.common.exceptions.OstrichException;
import com.ostrich.cloud.common.resultcode.CommonResultCode;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class AppContext {

    /**
     * 如果是登录或者注册请求，请求不会携带jwt，contextInfo也不会有值
     * @return
     */
    public static boolean initialized(){
        return ContextInfoHolder.getContextInfo()!=null;
    }

    public static int getOperatorId() {
        Integer operatorId = ContextInfoHolder.getContextInfo().getOperatorId();
        return isPresent(operatorId,"操作员ID不存在",Integer.class);
    }

    public static String getOperatorName() {
        String operatorName = ContextInfoHolder.getContextInfo().getOperatorName();
        return isPresent(operatorName,"操作员名字不存在",String.class);
    }

    public static <T> T isPresent(T data,String notPresentMessage,Class<T> type){
        if(data==null){
            throw new OstrichException(notPresentMessage, CommonResultCode.DATA_NOT_FOUND.getCode());
        }
        return data;
    }
}
